package com.mobileares.midp.widgets.client.textbox2;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.*;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-4-7
 * Time: 15:20:14
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseLabelText<T, U extends Widget> extends Composite implements HasValueChangeHandlers<Object> {
    private FlowPanel container;
    private U widget;
    private Label message;
    private Label error;
    private HTML labelWidget;

    public BaseLabelText(String label) {
        container = new FlowPanel();
        container.setStyleName("textbox-element");
        labelWidget = new HTML(label);
        labelWidget.setStyleName("label");
        container.add(labelWidget);
        initWidget(container);
    }

    public void setLabelWidget(U widget) {
        this.widget = widget;
        SimplePanel wrapper = new SimplePanel();
        wrapper.setStyleName("widget");
        wrapper.add(widget);
        container.add(wrapper);
        initEvent();
    }

    public void setAlertSign() {
        labelWidget.setHTML("<font color='red'>*</font>" + labelWidget.getText());
    }

    public U getLabelWidget() {
        return widget;
    }

    protected Widget getRealWidget() {
        return widget;
    }

    private void initEvent() {
        if (getRealWidget() instanceof HasAllFocusHandlers) {
            ((HasAllFocusHandlers) getRealWidget()).addFocusHandler(new FocusHandler() {
                public void onFocus(FocusEvent event) {
                    fireFocus(event);
                }
            });
            ((HasAllFocusHandlers) getRealWidget()).addBlurHandler(new BlurHandler() {
                public void onBlur(BlurEvent event) {
                    fireBlur(event);
                }
            });
        }
    }

    public void showTipMessage(String mess) {
        if (error != null) error.removeFromParent();
        message = new Label(mess);
        message.setStyleName("tip");
        container.add(message);
    }

    public void showErrorMessage(String mess) {
        if (message != null) message.removeFromParent();
        if (error == null) error = new Label();
        error.setText(mess);
        error.setStyleName("error");
        container.add(error);
        fireErrorMessage();
    }

    public void removeErrorMessage() {
        if (error != null) error.removeFromParent();
        if (message != null && !message.isAttached())
            container.add(message);
        fireRemoveErrorMessage();
    }

    protected void fireErrorMessage() {
        //todo
    }

    protected void fireRemoveErrorMessage() {
        //todo
    }

    protected void fireFocus(FocusEvent event) {
        //todo
    }

    protected void fireBlur(BlurEvent event) {
        //todo
    }

    public abstract Object getValue();

    public abstract void setValue(Object ob);

    protected void changeValue(Object newValue, Object oldValue) {
        if (newValue == null) {
            if (oldValue == null)
                return;
            else {
                ValueChangeEvent.fire(this, newValue);
            }
        } else {
            if (!newValue.equals(oldValue))
                ValueChangeEvent.fire(this, newValue);
        }
    }

    public boolean isValid() {
        if (error == null) return true;
        else
            return !error.isAttached();
    }

    public void reset() {
        removeErrorMessage();
        setValue(null);

    }

    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Object> handler) {
        // Is this the first value change handler? If so, time to add handlers
        return addHandler(handler, ValueChangeEvent.getType());
    }
}
